package com.xingkeduo.business.operation.domain;

import com.netflix.hystrix.contrib.javanica.utils.CommonUtils;
import com.xingkeduo.business.operation.service.*;
import com.xingkeduo.pojo.dto.CitiesDTO.CitiesDTO;
import com.xingkeduo.pojo.query.CitiesQuery;
import com.xingkeduo.pojo.query.ManagerQuery;
import com.xingkeduo.pojo.query.OfficeQuery;
import com.xingkeduo.pojo.query.ShopsQuery;
import com.xingkeduo.pojo.table.*;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 商户domain
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Component
public class CitiesDomain {

    @Autowired
    private CitiesService citiesService;

    @Autowired
    private CountryService countryService;

    @Autowired
    private VendorService vendorService;

    @Autowired
    private OfficeService officeService;

    @Autowired
    private ShopsService shopsService;


    public CitiesTable getCitiesInfoById(Long id) throws Throwable {

        CitiesTable citiesTable = citiesService.selectById(id);

        return citiesTable;
    }
    //获取列表
    public List<CitiesTable> getCitiesListByVendorId(CitiesQuery citiesQuery) throws Throwable {

        List<CitiesTable> citiesList = citiesService.selectLists(citiesQuery);

        return citiesList;
    }

    //分页查询
//    public Page<CitiesTable> getCititesPageList(CitiesQuery citiesQuery) throws Throwable {
//        Integer pageNo = citiesQuery.getPageNo();
//        Integer pageSize = citiesQuery.getPageSize();
//        Page<CitiesTable> postsTablePage = citiesService.selectPageLists(citiesQuery,pageNo,pageSize);
//        return postsTablePage;
//    }

    //分页查询测试
    public Page<CitiesDTO> getCititesPageList(CitiesQuery citiesQuery) throws Throwable {
        Integer pageNo = citiesQuery.getPageNo();
        Integer pageSize = citiesQuery.getPageSize();
        Page<CitiesTable> citiesTablePage = citiesService.selectPageLists(citiesQuery,pageNo,pageSize);

        List<CitiesTable> dtoList = citiesTablePage.getDtoList();

        ArrayList<CitiesDTO> objects = new ArrayList<>();
        if(!CommonUtil.isEmpty(dtoList)){
            for (CitiesTable citiesTable : dtoList) {
                CitiesDTO vendorDTO = new CitiesDTO();
                BeanUtils.copyProperties(citiesTable,vendorDTO);
                int officeNum = officeService.count(new OfficeQuery().setCitiesId(citiesTable.getId()).setStatus(1));
                vendorDTO.setOfficeNum(officeNum);
                int shopsNum = shopsService.count(new ShopsQuery().setCitiesId(citiesTable.getId()).setStatus(2));
                vendorDTO.setShopsNum(shopsNum);
                objects.add(vendorDTO);
            }
        }
        Page<CitiesDTO> officeDTOPage = new Page<>(pageNo,pageSize);
        BeanUtils.copyProperties(citiesTablePage, officeDTOPage);
        officeDTOPage.setDtoList(objects);
        return officeDTOPage;
    }


    public int createCities(CitiesTable citiesTable) throws Throwable {


        if(!CommonUtil.isEmpty(citiesTable.getCityid())){

           List<CitiesTable> citiesTableList=  citiesService.selectLists(new CitiesQuery().setCityid(citiesTable.getCityid()));
            if(!CommonUtil.isEmpty(citiesTableList)){
                throw new DataException(DataException.Type.DEFAULT, "该城市已存在，请重新选择!");
            }

        }

        CountryTable cityInfo = new CountryTable();
        CountryTable provInfo = new CountryTable();

        if(!CommonUtil.isEmpty(citiesTable.getProvid())){
            provInfo = countryService.selectById(citiesTable.getProvid());
        }
        if(!CommonUtil.isEmpty(provInfo)){
            citiesTable.setProvName(provInfo.getName());
        }

        if(!CommonUtil.isEmpty(citiesTable.getCityid())){
            cityInfo = countryService.selectById(citiesTable.getCityid());
        }
        if(!CommonUtil.isEmpty(cityInfo)){
            citiesTable.setCityName(cityInfo.getName());
            citiesTable.setName(cityInfo.getName());
        }

        VendorTable vendorTable = new VendorTable();

        if(!CommonUtil.isEmpty(citiesTable.getVendorId())){
            vendorTable = vendorService.selectById(citiesTable.getVendorId());
        }
        if(!CommonUtil.isEmpty(vendorTable)){
            citiesTable.setVendorName(vendorTable.getName());
        }

        return citiesService.insert(citiesTable);
    }

    public int modifyCities(CitiesTable citiesTable) throws Throwable {
        CountryTable cityInfo = new CountryTable();
        CountryTable provInfo = new CountryTable();
        CitiesTable citiesTable1 = citiesService.selectById(citiesTable.getId());

       if(!CommonUtil.isEmpty(citiesTable.getCityid())){
           if(!citiesTable1.getCityid().equals(citiesTable.getCityid())){
               if(!CommonUtil.isEmpty(citiesTable.getCityid())){
                   List<CitiesTable> citiesTableList=  citiesService.selectLists(new CitiesQuery().setCityid(citiesTable.getCityid()));
                   if(!CommonUtil.isEmpty(citiesTableList)){
                       throw new DataException(DataException.Type.DEFAULT, "该城市已经存在，请重新选择!");
                   }
               }
           }
       }
        //判断城市下分公司是否有状态为正常
        if(!CommonUtil.isEmpty(citiesTable.getStatus())){

            if(citiesTable.getStatus() == 2){
                List<OfficeTable> officeTableList= officeService.selectLists(new OfficeQuery().setCitiesId(citiesTable.getId()).setStatus(1));
                if(!CommonUtil.isEmpty(officeTableList) && officeTableList.size() > 0){
                    throw new DataException(DataException.Type.DEFAULT, "该城市下，还存在“启用”状态的分公司，无法停用城市!");
                }
            }else{

                if(!CommonUtil.isEmpty(citiesTable1)){
                    VendorTable vendorTable = vendorService.selectById(citiesTable1.getVendorId());
                    if(vendorTable.getStatus() ==2){
                        throw new DataException(DataException.Type.DEFAULT, "该城市所对应的大区处于禁用状态，请先启用对应大区!");
                    }
                }
            }

        }

        if(!CommonUtil.isEmpty(citiesTable.getProvid())){
            provInfo = countryService.selectById(citiesTable.getProvid());
        }
        if(!CommonUtil.isEmpty(provInfo)){
            citiesTable.setProvName(provInfo.getName());
        }

        if(!CommonUtil.isEmpty(citiesTable.getCityid())){
            cityInfo = countryService.selectById(citiesTable.getCityid());
        }
        if(!CommonUtil.isEmpty(cityInfo)){
            citiesTable.setCityName(cityInfo.getName());
            citiesTable.setName(citiesTable.getCityName());
        }

        VendorTable vendorTable = new VendorTable();
        if(!CommonUtil.isEmpty(citiesTable.getVendorId())){
            vendorTable = vendorService.selectById(citiesTable.getVendorId());
        }

        if(!CommonUtil.isEmpty(vendorTable)){
            citiesTable.setVendorName(vendorTable.getName());
        }

        return citiesService.update(citiesTable);
    }

    //删除
    public int delCities(Long id) throws Throwable {

        //判断城市下分公司是否有状态为正常
        if(!CommonUtil.isEmpty(id)){
            List<OfficeTable> officeTableList= officeService.selectLists(new OfficeQuery().setCitiesId(id).setStatus(1));
            if(!CommonUtil.isEmpty(officeTableList) && officeTableList.size() > 0){
                throw new DataException(DataException.Type.DEFAULT, "该城市下，还存在“启用”状态的分公司，无法删除该城市!");
            }
        }


        CitiesTable citiesTable = new CitiesTable();

        citiesTable.setId(id);
        citiesTable.setIsDelete(YesNoEnum.YES);
        int delRes = citiesService.delete(citiesTable);
        if(delRes > 0){
            return delRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "删除失败");
        }
    }

}
